package cn.jbolt.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Pager {


    private Integer pageNumber;//当前页数
    private Integer pageSize; //每页大小
    private Integer totalPage;//总记录数
    private Integer totalRow; //总页数
    private List list;
    // 计算总页数的方法
    public int getTotalPage() {
        if (pageSize == 0) {
            return 0; // 避免除以零的情况
        }
        return (int) Math.ceil((double) totalRow / pageSize);
    }


    public static Pager from(Page page) {
        Pager pager = new Pager();
        pager.setPageNumber((int) page.getCurrent());
        pager.setPageSize((int) page.getSize());
        pager.setTotalRow((int)page.getTotal());
        pager.setTotalPage(pager.getTotalPage());
        pager.list = page.getRecords();
        return pager;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(Integer totalRow) {
        this.totalRow = totalRow;
    }


}
